package LevelEditor.main;

import basis.game.main.Renderable;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Shape;
import static java.lang.Math.abs;
import static java.lang.Math.round;
import java.util.ArrayList;
import unamed.level.Tile;

public class Selection implements Renderable{

    private double x1,y1,x2,y2,ox1,oy1,ox2,oy2,osx,osy;
    private Plane p;
    private ArrayList<Renderable> items;
    
    public Selection(Plane p){
        this.p=p;
        items=new ArrayList<Renderable>();
    }
    
    public void setPoint1(double xx,double yy){
        double x=(xx<p.getX())?p.getX():(xx>p.getX()+p.getWidth())?p.getX()+p.getWidth():xx;
        double y=(yy<p.getY())?p.getY():(yy>p.getY()+p.getHeight())?p.getY()+p.getHeight():yy;
        osx=p.getX()-round(p.getX()/Tile.SIZE)*Tile.SIZE;
        osy=p.getY()-round(p.getY()/Tile.SIZE)*Tile.SIZE;
        x1=osx+round(x/Tile.SIZE)*Tile.SIZE;
        y1=osy+round(y/Tile.SIZE)*Tile.SIZE;
        ox1=x1-p.getX();
        oy1=y1-p.getY();
    }
    
    public void setPoint2(double xx,double yy){
        double x=(xx<p.getX())?p.getX():(xx>p.getX()+p.getWidth())?p.getX()+p.getWidth():xx;
        double y=(yy<p.getY())?p.getY():(yy>p.getY()+p.getHeight())?p.getY()+p.getHeight():yy;
        x2=osx+round(x/Tile.SIZE)*Tile.SIZE;
        y2=osy+round(y/Tile.SIZE)*Tile.SIZE;
        ox2=x2-p.getX();
        oy2=y2-p.getY();
    }
    
    public double getX1(){
        return x1;
    }
    
    public double getY1(){
        return y1;
    }
    
    public double getX2(){
        return x2;
    }
    
    public double getY2(){
        return y2;
    }
    
    public void setX1(double x){
        x1=x;
    }
    
    public void setY1(double y){
        y1=y;
    }
    
    public void setX2(double x){
        x2=x;
    }
    
    public void setY2(double y){
        y2=y;
    }
    
    @Override
    public double getX(){
        return (x1<x2)?x1:x2;
    }
    
    @Override
    public double getY(){
        return (y1<y2)?y1:y2;
    }
    
    public double getWidth(){
        return abs(x2-x1);
    }
    
    public double getHeight(){
        return abs(y2-y1);
    }
    
    @Override
    public void update(long t){
        x1=p.getX()+ox1;
        y1=p.getY()+oy1;
        x2=p.getX()+ox2;
        y2=p.getY()+oy2;
    }
    
    @Override
    public void render(Graphics2D g){
        g.setColor(Color.GREEN);
        g.drawRect((x1<x2)?(int)x1:(int)x2,(y1<y2)?(int)y1:(int)y2,(int)abs(x2-x1),(int)abs(y2-y1));
        g.drawLine((int)x1,(int)y1,(int)x2,(int)y2);
    }

    @Override
    public void setX(double x) {
        double cx=x2-x1;
        x1=x;
        x2=x+cx;
    }

    @Override
    public void setY(double y) {
        double cy=y2-y1;
        y1=y;
        y2=y+cy;
    }

    @Override
    public void show(boolean s) {
        
    }

    @Override
    public void move(double x, double y) {
        x1+=x;
        y1+=y;
        x2+=x;
        y2+=y;
    }

    @Override
    public boolean isShown() {
        return true;
    }

    @Override
    public Shape getBounds() {
        return null;
    }
    
}